﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.ServiceRuntime;

namespace DeepTime.Web
{
    public class Global : System.Web.HttpApplication
    {

        //void Application_BeginRequest(object sender, EventArgs e)
        //{
        //    //HttpApplication app = (HttpApplication)sender;
        //    //HttpContext context = app.Context;

        //    // Attempt to peform first request initialization
        //    //FirstRequestInitialization.Initialize();

        //}

        void Application_Start(object sender, EventArgs e)
        {
            #region Setup CloudStorageAccount Configuration Setting Publisher

            // This code sets up a handler to update CloudStorageAccount instances when their corresponding
            // configuration settings change in the service configuration file.
            CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) =>
            {
                // Provide the configSetter with the initial value
                configSetter(RoleEnvironment.GetConfigurationSettingValue(configName));

                RoleEnvironment.Changed += (sndr, arg) =>
                {
                    if (arg.Changes.OfType<RoleEnvironmentConfigurationSettingChange>()
                        .Any((change) => (change.ConfigurationSettingName == configName)))
                    {
                        // The corresponding configuration setting has changed, propagate the value
                        if (!configSetter(RoleEnvironment.GetConfigurationSettingValue(configName)))
                        {
                            // In this case, the change to the storage account credentials in the
                            // service configuration is significant enough that the role needs to be
                            // recycled in order to use the latest settings. (for example, the 
                            // endpoint has changed)
                            RoleEnvironment.RequestRecycle();
                        }
                    }
                };
            });

            #endregion
        }

        void Application_End(object sender, EventArgs e)
        {
            //  Code that runs on application shutdown
            
        }

        void Application_Error(object sender, EventArgs e)
        {
            // Code that runs when an unhandled error occurs

        }

        void Session_Start(object sender, EventArgs e)
        {
            // Code that runs when a new session is started

        }

        void Session_End(object sender, EventArgs e)
        {
            // Code that runs when a session ends. 
            // Note: The Session_End event is raised only when the sessionstate mode
            // is set to InProc in the Web.config file. If session mode is set to StateServer 
            // or SQLServer, the event is not raised.

        }

        //internal class FirstRequestInitialization
        //{
        //    private static bool s_InitializedAlready = false;
        //    private static Object s_lock = new Object();


        //    // Initialize only on the first request
        //    public static void Initialize()
        //    {
        //        if (s_InitializedAlready)
        //        {
        //            return;
        //        }

        //        lock (s_lock)
        //        {
        //            if (s_InitializedAlready)
        //            {
        //                return;
        //            }

        //            ApplicationStartUponFirstRequest();
        //            s_InitializedAlready = true;
        //        }
        //    }

        //    private static void ApplicationStartUponFirstRequest()
        //    {
        //        // This is where you put initialization logic for the site.
        //        // RoleManager is properly initialized at this point.

        //        // Create the tables on first request initialization as there is a performance impact
        //        // if you call CreateTableIfNotExist() when the tables already exist. This limits the exposure of
        //        // creating tables multiple times.

        //        // Get the settings from the Service Configuration file
        //        CloudStorageAccount storageAccount = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");

        //        // Create the tables
        //        storageAccount.CreateCloudTableClient().CreateTableIfNotExist("UploadedImageTable");
        //    }
        //}
    }
}
